<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-7-21
 * Time: 17:39
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class LetterController extends AdminBaseController
{
    public function index()
    {
        $param    = $this->request->param();
        $c_content = isset($param['c_content']) ? $param['c_content'] : '';
        $where['c.c_type'] = 40;
        if($c_content){ $where['c.c_content'] = ['like', "%$c_content%"];}else{ unset($param['c_content']);}

        $comment = DB::name('comment')
            ->field('c.id,c.c_cid,c.c_content,c.c_createtime,c.c_parent,c.c_status,ud.u_nickname,sl.s_name,count(c.id) as number')
            ->alias('c')
            ->join('UsersDetail ud','c.c_udid = ud.id')
            ->join('star_list sl','c.c_cid = sl.s_udid')
            ->where($where)
            ->group('c.c_parent,c.c_cid')
            ->order(['c.c_status' => 'ASC','c.id' => 'DESC'])
            ->paginate(20);
        $comment->appends($param);
        $page = $comment->render();
        $this->assign("c_content", $c_content);
        $this->assign("comment", $comment);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function info()
    {
        $param    = $this->request->param();
        $comment = DB::name('comment')
            ->field('c.id,c.c_content,c.c_createtime,c.c_status,ud1.u_nickname as cnickname,ud2.u_nickname as pnickname')
            ->alias('c')
            ->join('UsersDetail ud1','c.c_udid = ud1.id')
            ->join('UsersDetail ud2','c.c_parudid = ud2.id')
            ->where(["c.c_cid" => $param['cid'],'c.c_type'=>40,'c.c_parent'=>$param['parent']])
            ->order(['c.id' => 'ASC'])
            ->paginate(20);
        $comment->appends($param);
        $page = $comment->render();
        $this->assign("comment", $comment);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function disabled()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $shout = DB::name('comment')->where(["id" => $id])->find();
        switch ($shout['c_status']){
            case 20:
                DB::name('comment')->where(["id" => $id])->setField('c_status', 30);
                $this->success('您已屏蔽此留言');
                break;
            case 30:
                DB::name('comment')->where(["id" => $id])->setField('c_status', 20);
                $this->success('您已显示此留言');
                break;
            default:
                DB::name('comment')->where(["id" => $id])->setField('c_status', 30);
                $this->error('未知的状态,已置为屏蔽');
        }
    }
}